//
//  HMStatusCell.swift
//  iOSWeibo12
//
//  Created by Admin on 16/9/11.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit
import SDWebImage

class HMStatusCell: UITableViewCell {

    // MARK: - 属性
    /// 用户头像
    @IBOutlet weak var iconView: UIImageView!
    
    /// 用户名称Label
    @IBOutlet weak var nameLabel: UILabel!
    
    /// 来源Label
    @IBOutlet weak var sourceLabel: UILabel!
    
    /// 时间Label
    @IBOutlet weak var timeLabel: UILabel!
    
    /// 会员图标
    @IBOutlet weak var memberView: UIImageView!
    
    /// 认证图标
    @IBOutlet weak var verifiedView: UIImageView!
    
    /// 微博文本
    @IBOutlet weak var statusText: UILabel!
    
    @IBOutlet weak var pictureView: HMStatusPictureView!
    
    @IBOutlet weak var retweetStatusText: UILabel!
    
    /// 微博ViewModel
    var statusVM: HMStatusViewModel? {
        didSet {
            guard let status = statusVM?.status else {
                print("statusVM中没有微博")
                return
            }
            
            // 头像,使用SDWebImage设置网络图片内容
            let urlString = status.user?.profile_image_url ?? ""
            HMAsyncDrawImage.shared.hm_AsyncDrawImage(urlString: urlString) { (image) in
                self.iconView.image = image
            }
//            let iconURL = URL(string: status.user?.profile_image_url ?? "")
//            iconView.sd_setImage(with: iconURL, placeholderImage: UIImage(named: "avator"))
            
            // 用户名称
            nameLabel.text = status.user?.name ?? "未知用户名称"
            
            // 来源
//            sourceLabel.text = status.source ?? "未知来源"
//            sourceLabel.text = "来自 ***"
            sourceLabel.text = statusVM?.statusSource
            
            // 时间
//            timeLabel.text = status.created_at ?? "未知时间"
            timeLabel.text = statusVM?.sinaDateDescription ?? "未知时间"
            
            // 微博内容
            statusText.attributedText = statusVM?.originalAttrText
            
            // 会员等级
            memberView.image = statusVM?.mbrankImage
            
            // 认证图标
            verifiedView.image = statusVM?.verifiedImage
            
            // 判断是否是被转发微博
            if statusVM!.isRetweetStatus {
                // 是被转发微博,设置被转发微博文字和图片
                let retweetName = status.retweeted_status?.user?.name ?? "没有名称"
                let retweetText = status.retweeted_status?.text ?? "没有被转发微博"
                retweetStatusText.attributedText = statusVM?.retweetAttrText
                pictureView.pictureURLs = statusVM?.retweetStorePictureURLs
            } else {
                // 设置原创微博图片
                pictureView.pictureURLs = statusVM?.storePictureURLs
            }
        }
    }
    
    // MARK: - 方法
    override func awakeFromNib() {
        super.awakeFromNib()
        
//        iconView.layer.cornerRadius = 17.5
//        iconView.layer.masksToBounds = true
        
//        memberView.layer.shadowColor = UIColor.green.cgColor
//        memberView.layer.shadowOffset = CGSize(width: 1, height: 1)
//        memberView.layer.shadowRadius = 1
//        memberView.layer.shadowOpacity = 0.8
        
        self.selectionStyle = UITableViewCellSelectionStyle.none
    }
}
